﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Data;

namespace BingDesktopChanger
{
    internal class DBImageManager : IImageManager
    {
        private const string DELETE_IMAGES_BY_NAME = "DELETE FROM tblImages WHERE imageName = '{0}'";
        private const string DELETE_IMAGES_BY_DATE = "DELETE FROM tblImages WHERE publicationDate = '{0}'";
        private const string EXISTS_IMAGES_BY_DATE = "SELECT COUNT(1) FROM tblImages WHERE publicationDate = '{0}'";
        private const string EXISTS_IMAGES_BY_NAME = "SELECT COUNT(1) FROM tblImages WHERE imageName = '{0}'";
        private const string GET_IMAGES_BY_DATE = "SELECT imageName FROM tblImages WHERE publicationDate = '{0}'";
        private const string SAVE_IMAGE = "INSERT INTO tblImages(imageName, publicationDate) VALUES('{0}','{1}')";

        private IImageManager nextManager;


        public DBImageManager(IImageManager nextManager)
        {
            this.nextManager = nextManager;
        }

        #region IImageManager Members
        public void DeleteImage(string imageName)
        {
            new DataContext().ExecuteSentence(string.Format(DELETE_IMAGES_BY_NAME, imageName.ToUpper()));
            if (this.nextManager != null)
                this.nextManager.DeleteImage(imageName);
        }
        public void DeleteImages(DateTime targetDay)
        {
            //if (this.nextManager != null)
            //    foreach (var image in this.GetImageList(targetDay))
            //        this.nextManager.DeleteImage(image);
            new DataContext().ExecuteSentence(string.Format(DELETE_IMAGES_BY_DATE, Helper.DateToDbFormat(targetDay)));
        }
        public void DeleteImages(DateTime fromDate, DateTime toDate)
        {
            var workDate = fromDate;
            while (workDate <= toDate)
            {
                this.DeleteImages(workDate);
                workDate = workDate.AddDays(1);
            }
        }
        public bool ExistsDayImages(DateTime targetDay)
        {
            var sentence = string.Format(EXISTS_IMAGES_BY_DATE, Helper.DateToDbFormat(targetDay));
            object value = new DataContext().ExecuteScalar(sentence);
            return Convert.ToInt32(value ?? 0) > 0;
        }
        public bool ExistsImage(string imageName)
        {
            var sentence = string.Format(EXISTS_IMAGES_BY_NAME, imageName.ToUpper());
            object value = new DataContext().ExecuteScalar(sentence);
            if (Convert.ToInt32(value ?? 0) > 0)
                return true;

            if (this.nextManager != null)
                return this.nextManager.ExistsImage(imageName);

            return false;
        }
        public string[] GetImageList(DateTime targetDay)
        {
            string query = string.Format(GET_IMAGES_BY_DATE, Helper.DateToDbFormat(targetDay));
            List<string> list = new List<string>();
            var table = new DataContext().ExecuteDataTable(query);
            foreach (DataRow row in table.Rows)
                list.Add(row[0].ToString());
            //using (var rdr = new DataContext().ExecuteDataReader(query))
            //    while (rdr.Read())
            //        list.Add(rdr.GetString(0));
            return list.ToArray();
        }
        public void SaveImage(Stream imageContent, DateTime targetDay, string imageName)
        {
            var sentence = string.Format(SAVE_IMAGE, imageName.ToUpper(), Helper.DateToDbFormat(targetDay));
            new DataContext().ExecuteSentence(sentence);
            if (this.nextManager != null)
                this.nextManager.SaveImage(imageContent, targetDay, imageName);
        }
        public void SetChainedManager(IImageManager manager)
        {
            this.nextManager = manager;
        }
        #endregion

    }
}
